package com.dianjing.dianqun.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.dianjing.dianqun.common.base.CrudService;
import com.dianjing.dianqun.dao.UserExtMapper;
import com.dianjing.dianqun.dao.UserMapper;
import com.dianjing.dianqun.model.FriendsRelation;
import com.dianjing.dianqun.model.User;
import com.dianjing.dianqun.model.UserExt;
import com.dianjing.dianqun.model.dto.UserDTO;
import com.dianjing.dianqun.model.dto.UserFlowDTO;

/**
 * @date : 2017年10月11日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */
@Service
@Transactional
public class UserService extends CrudService<UserMapper, User> {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserExtMapper extMapper;

	@Autowired
	private FriendsRelationService friendsRelationService;

	/**
	 * 获取我关注的人 列表
	 * 
	 * @param id
	 * @return
	 */
	public List<UserDTO> selectFriendUser(Integer id) {
		return userMapper.selectFriendUser(id);
	}

	/**
	 * 获取关注我的人 列表
	 * 
	 * @param id
	 * @return
	 */
	public List<UserDTO> selectFollowMeUser(Integer id) {
		return userMapper.selectFollowMeUser(id);
	}

	/**
	 * 获取用户ext信息
	 * 
	 * @param id
	 * @return
	 */
	public UserExt selectUserExtInfo(Integer id) {
		return extMapper.selectByPrimaryKey(id);
	}

	/**
	 * 关注 方法
	 * 
	 * @param userId
	 *            关注者
	 * @param followId
	 *            被关注者
	 * @return 是否成功
	 */
	@Transactional(readOnly = false, isolation = Isolation.DEFAULT)
	public Boolean followUser(Integer userId, Integer followId) {
		// 获取被关注者
		User followUser = userMapper.selectByPrimaryKey(followId);
		if (null != followUser) {
			// 查询我关注的 用户 followId
			FriendsRelation friendsRelation = friendsRelationService.selectMyFlowUser(userId, followId);
			if (friendsRelation == null) {
				// 为空则不存在我关注的关系
				FriendsRelation relation = new FriendsRelation();
				relation.setFriendId(followId);
				relation.setUserId(userId);
				relation.setBothFriends(true);
				friendsRelationService.insertSelective(relation);
			} else {
				// 相互狀態改變
				FriendsRelation f = friendsRelationService.selectMyFlowUser(followId, userId);
				if (null != f) {
					f.setBothFriends(true);
					friendsRelationService.updateByPrimaryKey(f);
				}
				friendsRelation.setBothFriends(true);
				friendsRelationService.updateByPrimaryKey(friendsRelation);
			}
			return true;
		}
		return false;
	}

	// 查看是否关注
	@Transactional(readOnly = false, isolation = Isolation.DEFAULT)
	public Boolean isFolow(Integer userId, Integer followId) {
		if (userId.equals(followId)) {
			return true;
		}
		FriendsRelation friendsRelation = friendsRelationService.selectMyFlowUser(userId, followId);
		if (null == friendsRelation) {
			return false;
		}
		return true;
	}

	/**
	 * 根据 用户id（点睛号） 或者 手机号查询好友
	 * 
	 * @param param
	 * @return
	 */
	public User selectByIdOrPhone(String param) {
		return userMapper.selectByIdOrPhone(param);
	}

	/**
	 * 查询用户粉丝
	 * 
	 * @param userId
	 * @return
	 */
	public Integer selectFansByUserId(Integer userId) {
		return userMapper.selectFansByUserId(userId);
	}

	/**
	 * 取消关注
	 * 
	 * @param userId
	 * @param followId
	 * @return
	 */
	public Boolean cancelFollowUser(Integer userId, Integer followId) {
		// 获取被关注者
		User followUser = userMapper.selectByPrimaryKey(followId);
		if (null != followUser) {
			FriendsRelation friendsRelation = friendsRelationService.selectMyFlowUser(userId, followId);
			if (friendsRelation != null) {
				// 为空则不存在我关注的关系
				FriendsRelation f = friendsRelationService.selectMyFlowUser(followId, userId);
				if (f != null) {
					f.setBothFriends(false);
					friendsRelationService.updateByPrimaryKey(f);
				}
				friendsRelation.setBothFriends(false);
				friendsRelationService.deleteByPrimaryKey(friendsRelation.getFriendsRelationId());
			}
			return true;
		}
		return false;
	}

	/**
	 * 根据 站点id查询用户的最近访客
	 * 
	 * @param userId
	 * @return
	 */
	public List<UserFlowDTO> selectLatelyUserList(Integer siteId,Integer userId , Integer type) {
		return userMapper.selectLatelyUserList(siteId,userId,type);
	}

	/**
	 * 查询互相关注的人
	 * 
	 * @param id
	 * @return
	 */
	public List<UserDTO> selectUserByHXGZ(Integer id) {
		return userMapper.selectUserByHXGZ(id);
	}

	/**
	 * 根据站点id查询对应的用户
	 * 
	 * @param siteId
	 * @return
	 */
	public User selectUserBySiteId(Integer siteId) {
		return userMapper.selectUserBySiteId(siteId);
	}
	/**
	 * 根据站点关键字搜索查询对应的用户
	 * 
	 * @param keyword 关键字
	 * @return
	 */
	public UserDTO searchUserByKeyword(String keyword) {
		return userMapper.searchUserByKeyword(keyword);
	}
	/**
	 * 根据站点id查询对应的用户基本信息
	 * 
	 * @param siteId
	 * @return
	 */
	public UserDTO searchUserBySiteId(Integer siteId) {
		return userMapper.searchUserBySiteId(siteId);
	}

	/**
	 * 夸数据源查询 转换用户 
	 * @return
	 */
	public UserDTO convertUser(Integer userId) {
		return userMapper.convertUser(userId);
	}

}
